n,m = map(int,input().split(" "))
l1 = []
l2 = []
for i in range(n):
c,d = map(int,input().split(" "))
l1.append(c)
l2.append(d)
for i in range(n):
if(l1[i] > l2[i]):
s = l1[i]
l1[i] = l2[i]
l2[i] = s
l1.sort()
l2.sort()
a = l1[n-1]
b = l2[0]
if(a>b):
print(-1)
elif(a > m):
print(a-m)
elif(b < m):
print(m-b)
else:
print(0)
#include <cstdio>
int main(){
int n, x0; scanf("%d %d\n", &n, &x0);
int m(0), M(1001);
while(n--){
int a, b; scanf("%d %d\n", &a, &b);
if(a > b){int temp(a); a = b; b = temp;}
if(b < M){M = b;}
if(a > m){m = a;}
}
if(m > M){puts("-1");}
else{
int ans = 1001;
if(m <= x0 && x0 <= M){ans = 0;}
if(x0 < m && ans > m - x0){ans = m - x0;}
if(x0 > M && ans > x0 - M){ans = x0 - M;}
printf("%d\n", ans);
}
return 0;
}
Missing numbers | Maximum sum |
13 Reasons Why | Friend's Relationship |
Health of a person | Divisibility |
A. Movement | Numbers in a matrix |
Sequences | Split houses |
Divisible | Three primes |
Coprimes | Cost of balloons |
One String No Trouble | Help Jarvis! |
Lift queries | Goki and his breakup |
Ali and Helping innocent people | Book of Potion making |
Duration | Birthday Party |
e-maze-in | Bricks Game |
Char Sum | Two Strings |
Anagrams | Prime Number |
Lexical Sorting Reloaded | 1514A - Perfectly Imperfect Array |